package org.jeecg.modules.tdm.service.impl;

import org.jeecg.modules.tdm.entity.TestMain;
import org.jeecg.modules.tdm.entity.TestValue;
import org.jeecg.modules.tdm.entity.TestFiles;
import org.jeecg.modules.tdm.mapper.TestValueMapper;
import org.jeecg.modules.tdm.mapper.TestFilesMapper;
import org.jeecg.modules.tdm.mapper.TestMainMapper;
import org.jeecg.modules.tdm.service.ITestMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: bus_test_main
 * @Author: jeecg-boot
 * @Date:   2022-04-05
 * @Version: V1.0
 */
@Service
public class TestMainServiceImpl extends ServiceImpl<TestMainMapper, TestMain> implements ITestMainService {

	@Autowired
	private TestMainMapper testMainMapper;
	@Autowired
	private TestValueMapper testValueMapper;
	@Autowired
	private TestFilesMapper testFilesMapper;
	
	@Override
	@Transactional
	public void saveMain(TestMain testMain, List<TestValue> testValueList,List<TestFiles> testFilesList) {
		testMainMapper.insert(testMain);
		if(testValueList!=null && testValueList.size()>0) {
			for(TestValue entity:testValueList) {
				//外键设置
				entity.setTestMainId(testMain.getId());
				testValueMapper.insert(entity);
			}
		}
		if(testFilesList!=null && testFilesList.size()>0) {
			for(TestFiles entity:testFilesList) {
				//外键设置
				entity.setTestMainId(testMain.getId());
				testFilesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(TestMain testMain,List<TestValue> testValueList,List<TestFiles> testFilesList) {
		testMainMapper.updateById(testMain);
		
		//1.先删除子表数据
		testValueMapper.deleteByMainId(testMain.getId());
		testFilesMapper.deleteByMainId(testMain.getId());
		
		//2.子表数据重新插入
		if(testValueList!=null && testValueList.size()>0) {
			for(TestValue entity:testValueList) {
				//外键设置
				entity.setTestMainId(testMain.getId());
				testValueMapper.insert(entity);
			}
		}
		if(testFilesList!=null && testFilesList.size()>0) {
			for(TestFiles entity:testFilesList) {
				//外键设置
				entity.setTestMainId(testMain.getId());
				testFilesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		testValueMapper.deleteByMainId(id);
		testFilesMapper.deleteByMainId(id);
		testMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			testValueMapper.deleteByMainId(id.toString());
			testFilesMapper.deleteByMainId(id.toString());
			testMainMapper.deleteById(id);
		}
	}
	
}
